package com.ruoyi.project.business.salaryrule.controller;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.business.salaryrule.request.EditMonthSalaryRuleRequest;
import com.ruoyi.project.system.entity.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.business.salaryrule.request.ListMonthSalaryRuleRequest;
import com.ruoyi.project.business.salaryrule.request.AddMonthSalaryRuleRequest;
import com.ruoyi.project.business.salaryrule.response.MonthSalaryRuleResponse;
import com.ruoyi.project.business.salaryrule.service.IMonthSalaryRuleService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.model.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 月薪计算规则Controller
 * 
 * @author ${author}
 * @date 2024-02-21
 */
@RestController
@RequestMapping("/business/salaryrule")
public class MonthSalaryRuleController extends BaseController {
    @Autowired
    private IMonthSalaryRuleService monthSalaryRuleService;

    /**
     * 查询月薪计算规则列表
     */
    @PreAuthorize("@ss.hasPermi('business:salaryrule:list')")
    @GetMapping("/list")
    public TableDataInfo list(ListMonthSalaryRuleRequest request) {
        startPage();
        Long tenantId = getTenantId();
        List<MonthSalaryRuleResponse> list = monthSalaryRuleService.selectMonthSalaryRuleList(request, tenantId);
        return getDataTable(list);
    }

    /**
     * 导出月薪计算规则列表
     */
    @PreAuthorize("@ss.hasPermi('business:salaryrule:export')")
    @Log(title = "月薪计算规则", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ListMonthSalaryRuleRequest request) {
        Long tenantId = getTenantId();
        List<MonthSalaryRuleResponse> list = monthSalaryRuleService.selectMonthSalaryRuleList(request, tenantId);
        ExcelUtil<MonthSalaryRuleResponse> util = new ExcelUtil<>(MonthSalaryRuleResponse.class);
        return util.exportExcel(list, "salaryrule");
    }

    /**
     * 获取月薪计算规则详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:salaryrule:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(monthSalaryRuleService.selectMonthSalaryRuleById(id));
    }

    /**
     * 新增月薪计算规则
     */
    @PreAuthorize("@ss.hasPermi('business:salaryrule:add')")
    @Log(title = "月薪计算规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AddMonthSalaryRuleRequest request) {
        Long tenantId = getTenantId();
        Long operatorId = getUserId();
        SysUser sysUser = SecurityUtils.getSysUser();
        Long deptId = sysUser.getDeptId();

        return toAjax(monthSalaryRuleService.insertMonthSalaryRule(request, deptId, operatorId, tenantId));
    }

    /**
     * 修改月薪计算规则
     */
    @PreAuthorize("@ss.hasPermi('business:salaryrule:edit')")
    @Log(title = "月薪计算规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EditMonthSalaryRuleRequest request) {
        Long tenantId = getTenantId();
        Long operatorId = getUserId();
        return toAjax(monthSalaryRuleService.updateMonthSalaryRule(request, operatorId, tenantId));
    }

    /**
     * 删除月薪计算规则
     */
    @PreAuthorize("@ss.hasPermi('business:salaryrule:remove')")
    @Log(title = "月薪计算规则", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        Long tenantId = getTenantId();
        Long operatorId = getUserId();
        return toAjax(monthSalaryRuleService.deleteMonthSalaryRuleByIds(ids, operatorId, tenantId));
    }
}
